import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/widgets/chart/pie/base/base_pie_entity.dart';
import 'package:flutter_widgets_pc/widgets/chart/pie/kq_pie_chart_3d.dart';
import 'package:flutter_widgets_pc/widgets/titleBar/kq_title_bar.dart';

import 'package:flutter_widgets_pc/widgets/chart/pie/kq_pie_chart.dart';

/// 饼状图
class PieChart3DPage extends StatelessWidget {
  const PieChart3DPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: '3D饼状图',
        showBack: false,
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart2(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart3(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: Text(
                "下面为两种无数据的展示方式：",
                style: TextStyle(fontSize: 20),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar2(),
            ),
          )
        ],
      ),
    );
  }

  /// 常规饼状图
  Widget buildPieChart() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqPieChart3D(
        animDuration: const Duration(seconds: 2),
        radius: 90,
        height: 30,
        gestureHandler: KqPieChart3DGestureHandler(),
        highLightPainter: KqPieChart3DHighLightPainter(
            offsetRadius: 0,
            colorBuilder: (p0) {
              return HSLColor.fromColor(p0.color).withLightness(0.7).toColor();
            }),
        data: [
          KqPieChartEntity(
              color: Colors.redAccent,
              value: 10,
              drawLabel: true,
              label: "测试",
              labelSize: 10,
              labelColor: Colors.white,
              secondLabel: "测试2",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.blueAccent,
              value: 33.5,
              drawLabel: true,
              label: "测试1",
              labelSize: 10,
              labelColor: Colors.white,
              secondLabel: "测试12",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.brown,
              value: 10,
              drawLabel: true,
              label: "测试2",
              labelSize: 10,
              labelColor: Colors.white,
              secondLabel: "测试22",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.lightGreen,
              value: 19.3,
              drawLabel: true,
              label: "测试3",
              labelSize: 10,
              labelColor: Colors.white,
              secondLabel: "测试32",
              secondLabelSize: 8)
        ],
        labelPosition: LabelPosition.inside(),
        sliceSpace: 0,
        innerRadius: 0,
      ),
    );
  }

  /// 圆环饼状图
  Widget buildPieChart2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqPieChart3D.withDefault(
        animDuration: const Duration(seconds: 2),
        radius: 90,
        height: 30,
        data: [
          KqPieChartEntity(
              color: Colors.redAccent,
              value: 10,
              drawLabel: true,
              label: "测试",
              labelSize: 10,
              labelColor: Colors.redAccent,
              secondLabel: "测试2",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.blueAccent,
              value: 33.5,
              drawLabel: true,
              label: "测试1",
              labelSize: 10,
              labelColor: Colors.blueAccent,
              secondLabel: "测试12",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.brown,
              value: 10,
              drawLabel: true,
              label: "测试2",
              labelSize: 10,
              labelColor: Colors.brown,
              secondLabel: "测试22",
              secondLabelSize: 8),
          KqPieChartEntity(
              color: Colors.lightGreen,
              value: 19.3,
              drawLabel: true,
              label: "测试3",
              labelSize: 10,
              labelColor: Colors.lightGreen,
              secondLabel: "测试32",
              secondLabelSize: 8)
        ],
        labelPosition: LabelPosition.outside(
          lineFactory: (entity) {
            return IndicatorLine(
                color: entity.color,
                drawStartCircle: true,
                circleColor: entity.color,
                offset: 4);
          },
        ),
        sliceSpace: 4,
        innerRadius: 60,
      ),
    );
  }

  /// 丁达尔玫瑰图
  Widget buildPieChart3() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqPieChart3D(
        animDuration: const Duration(seconds: 2),
        radius: 90,
        height: 30,
        gestureHandler: KqPieChart3DGestureHandler(),
        highLightPainter:
            KqPieChart3DHighLightPainter(addedRadius: 20, offsetRadius: 0),
        data: [
          KqPieChartEntity(color: Colors.redAccent, value: 10),
          KqPieChartEntity(color: Colors.brown, value: 15, addedRadius: 10),
          KqPieChartEntity(
              color: Colors.lightGreen, value: 20, addedRadius: 20),
          KqPieChartEntity(
              color: Colors.blueAccent, value: 25, addedRadius: 30),
        ],
        sliceSpace: 0,
        innerRadius: 60,
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar() {
    return Container(
      width: 160,
      height: 160,
      color: Colors.lightBlue[200],
      child: KqPieChart3D.withDefault(radius: 70),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqPieChart3D(
        radius: 90,
        innerRadius: 60,
        baseMapColor: Colors.grey,
        height: 30,
        gestureHandler: KqPieChart3DGestureHandler(),
      ),
    );
  }
}
